﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class BaseData_PersonalIncomeTax : System.Web.UI.Page
{
    private DataTable PersonalIncomeTax
    {
        get
        {
            DataTable personalIncomeTax = null;
            if (ViewState["PersonalIncomeTax"] == null)
            {
                ViewState["PersonalIncomeTax"] = new BLL.PersonalIncomeTax().GetAllTax();
            }
            personalIncomeTax = (DataTable)ViewState["PersonalIncomeTax"];
            return personalIncomeTax;
        }

        set
        {
            ViewState["PersonalIncomeTax"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dlistPersonalIncomeTax.DataSource = this.PersonalIncomeTax;
            dlistPersonalIncomeTax.DataBind();
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消吗?'))return false;");
        }
    }

    protected void dlistPersonalIncomeTax_CancelCommand(object source, DataListCommandEventArgs e)
    {
        dlistPersonalIncomeTax.EditItemIndex = -1;
        dlistPersonalIncomeTax.DataSource = this.PersonalIncomeTax;
        dlistPersonalIncomeTax.DataBind();
    }
    protected void dlistPersonalIncomeTax_EditCommand(object source, DataListCommandEventArgs e)
    {
        dlistPersonalIncomeTax.EditItemIndex = e.Item.ItemIndex;
        dlistPersonalIncomeTax.DataSource = this.PersonalIncomeTax;
        dlistPersonalIncomeTax.DataBind();
    }
    protected void dlistPersonalIncomeTax_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (!Check.CheckPermission("200600110", "00003"))
        {
            Share.Alert(this.Page, "无权操作！"); return;
        }
        if (e.CommandName.ToLower() == "del")
        {
            this.PersonalIncomeTax.Rows.RemoveAt(e.Item.ItemIndex);
        }
        dlistPersonalIncomeTax.DataSource = this.PersonalIncomeTax;
        dlistPersonalIncomeTax.DataBind();
    }
    protected void dlistPersonalIncomeTax_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        try
        {
            TextBox txtStandardDeduction = (TextBox)e.Item.FindControl("txtStandardDeduction");
            TextBox txtLowerLimit = (TextBox)e.Item.FindControl("txtLowerLimit");
            TextBox txtUpperLimit = (TextBox)e.Item.FindControl("txtUpperLimit");
            TextBox txtRate = (TextBox)e.Item.FindControl("txtRate");
            TextBox txtDeductibleAmount = (TextBox)e.Item.FindControl("txtDeductibleAmount");
            this.PersonalIncomeTax.Rows[e.Item.ItemIndex]["StandardDeduction"] = Convert.ToDecimal(txtStandardDeduction.Text);
            this.PersonalIncomeTax.Rows[e.Item.ItemIndex]["LowerLimit"] = Convert.ToDecimal(txtLowerLimit.Text);
            this.PersonalIncomeTax.Rows[e.Item.ItemIndex]["UpperLimit"] = Convert.ToDecimal(txtUpperLimit.Text);
            this.PersonalIncomeTax.Rows[e.Item.ItemIndex]["Rate"] = Convert.ToDecimal(txtRate.Text);
            this.PersonalIncomeTax.Rows[e.Item.ItemIndex]["DeductibleAmount"] = Convert.ToDecimal(txtStandardDeduction.Text);

            dlistPersonalIncomeTax.EditItemIndex = -1;
            dlistPersonalIncomeTax.DataSource = this.PersonalIncomeTax;
            dlistPersonalIncomeTax.DataBind();
        }
        catch (FormatException ex)
        {
            Share.Alert(this.Page, "请输入正确的数字！");
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            dlistPersonalIncomeTax.DataSource = new BLL.PersonalIncomeTax().AddNewColumn(this.PersonalIncomeTax, Convert.ToDecimal(txtStandardDeduction.Text),
                Convert.ToDecimal(txtLowerLimit.Text), Convert.ToDecimal(txtUpperLimit.Text), Convert.ToDecimal(txtRate.Text), Convert.ToDecimal(txtDeductibleAmount.Text));
            dlistPersonalIncomeTax.DataBind();
        }
        catch (FormatException ex)
        {
            Share.Alert(this.Page, "请输入正确的数字！");
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.PersonalIncomeTax.Rows.Count == 0)
        {
            Share.Alert(this.Page,"请添加个人所得税标准！"); return;
        }
        StringBuilder sbSql = new StringBuilder();
        sbSql.Append("delete from PersonalIncomeTax|");

        for (int i = 0; i < this.PersonalIncomeTax.Rows.Count; i++)
        {
            sbSql.Append("insert into PersonalIncomeTax([LowerLimit],[UpperLimit],[Rate],[DeductibleAmount],[StandardDeduction])VALUES(");
            sbSql.Append(this.PersonalIncomeTax.Rows[i]["LowerLimit"].ToString());
            sbSql.Append(",");
            sbSql.Append(this.PersonalIncomeTax.Rows[i]["UpperLimit"].ToString());
            sbSql.Append(",");
            sbSql.Append(this.PersonalIncomeTax.Rows[i]["Rate"].ToString());
            sbSql.Append(",");
            sbSql.Append(this.PersonalIncomeTax.Rows[i]["DeductibleAmount"].ToString());
            sbSql.Append(",");
            sbSql.Append(this.PersonalIncomeTax.Rows[i]["StandardDeduction"].ToString());
            sbSql.Append(")|");
        }
        string stringSql = sbSql.ToString();
        char[] charArray = new char[] { '|' };
        string[] sqlCommandArray = stringSql.TrimEnd(charArray).Split(charArray);
        if (new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray))
        {
            Share.InsertLog("200600110", "00017");
            Response.Write("<script>alert('保存成功！');window.location.href='PersonalIncomeTax.aspx'</script>");
        }
        else
        {
            Share.Alert(this.Page, "保存失败！");
        }
        
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("PersonalIncomeTax.aspx");
    }
}
